package com.xtz.handler;

import com.xtz.vo.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * date: 2022/5/10 15:17
 *全局异常处理
 * @author xietianze
 */
//1.声明成一个bean
// 2.声明为一个全局异常不获取
@ControllerAdvice(annotations = {RestController.class})
//响应的所有数据，都以字符串形式返回
@ResponseBody
public class GlobalExceptionHandler {
     //当系统中任意位置，出现Exception的异常
    //那么这个异常会被这个方法给捕获   (要捕获的异常类型)
     @ExceptionHandler(Exception.class)
     public Result exceptionHandler(Exception ex){
         System.out.println(ex.getMessage());

         return new Result(500,ex.getMessage(),null,null);
     }

    /**
     * 异常处理方法
     * @return
     *//*
    @ExceptionHandler(CustomException.class)
    public Result exceptionHandler(CustomException ex){
        System.out.println(ex.getMessage());

        return new Result(500,ex.getMessage(),null,null);
    }*/
}
